package com.sr.examination.exam.common.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sr.examination.R;
import com.tl.commonlibrary.ui.adapter.IAdapter;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.beans.ExerciseBean;

import java.util.List;

/**
 * Created by JongSung on 18/2/10.
 */

public class NumberCardAdapter extends IAdapter<ExerciseBean> {
    private int start = 0;
    private boolean clickable = false;

    public NumberCardAdapter(Context context, List datas) {
        super(context, datas, R.layout.item_number_card);
    }

    public NumberCardAdapter setStart(int start) {
        this.start = start;
        return this;
    }

    public void setClickable(boolean clickable) {
        this.clickable = clickable;
    }

    @Override
    protected void getView(ViewHolder viewHolder, ExerciseBean entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        final int numberCard = start + position + 1;
        TextView numberCardTView = viewHolder.getView(R.id.numberCardTView);
        numberCardTView.setText(String.valueOf(numberCard));
        int bgResId = R.drawable.bg_circle_gray_stroke;
        int txtColorId = R.color.base_gray;
        if (entity.answerRight()) {
            bgResId = R.drawable.bg_circle_green_solid;
            txtColorId = R.color.white;
        } else if (entity.answerWrong()) {
            bgResId = R.drawable.bg_circle_deep_red_solid;
            txtColorId = R.color.white;
        } else if (entity.hasAnswer()) {
            bgResId = R.drawable.bg_circle_blue_solid;
            txtColorId = R.color.white;
        } else if (entity.hasMyScore()) {
            bgResId = R.drawable.bg_circle_yellow_solid;
            txtColorId = R.color.white;
        }
        numberCardTView.setBackgroundResource(bgResId);
        numberCardTView.setTextColor(ContextCompat.getColor(context, txtColorId));
        if (clickable) {
            numberCardTView.setTag(R.id.tag_id, numberCard);
            if (!isConvert) {
                numberCardTView.setOnClickListener(this);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.numberCardTView:
                if (context instanceof Activity) {
                    Intent intent = new Intent();
                    intent.putExtra("NumberCard", (int) v.getTag(R.id.tag_id));
                    ((Activity) context).setResult(Activity.RESULT_OK, intent);
                    ((Activity) context).finish();
                }
                break;
        }
    }
}
